package com.edu.exam.entity.examination.paper.technical;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import jakarta.persistence.*;
import java.time.LocalDateTime;

// 导入外部枚举类
import com.edu.exam.enums.examination.paper.ExamNumberRecognitionMethod;
import com.edu.exam.enums.examination.paper.ExamNumberFormatValidation;
import com.edu.exam.enums.examination.paper.ExamNumberStudentMatchStatus;
import com.edu.exam.enums.examination.paper.ExamNumberManualVerificationResult;

/**
 * 考号识别结果实体类
 *
 * 根据E-04-03 OMR识别子模块数据库设计实现
 * 存储考号识别的详细信息，包括识别结果、置信度、识别方法、校验信息等。
 *
 * @author System
 * @version 1.0.0
 */

@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class ExamNumberResult {

    /**
     * 考号识别ID，主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "exam_number_id")
    private Long examNumberId;

    /**
     * 关联信息
     */
    @Column(name = "result_id", nullable = false)
    private Long resultId;

    @Column(name = "sheet_id", nullable = false)
    private Long sheetId;

    /**
     * 考号识别结果
     */
    @Column(name = "recognized_number", length = 100)
    private String recognizedNumber;

    @Column(name = "raw_number_region", columnDefinition = "JSON")
    private String rawNumberRegion;

    @Enumerated(EnumType.STRING)
    @Column(name = "recognition_method", length = 20)
    private ExamNumberRecognitionMethod recognitionMethod;

    /**
     * 置信度信息
     */
    @Column(name = "overall_confidence", precision = 5, scale = 2)
    private Double overallConfidence;

    @Column(name = "digit_confidences", columnDefinition = "JSON")
    private String digitConfidences;

    @Column(name = "confidence_distribution", columnDefinition = "JSON")
    private String confidenceDistribution;

    /**
     * 识别质量
     */
    @Column(name = "clarity_score", precision = 5, scale = 2)
    private Double clarityScore;

    @Column(name = "completeness_score", precision = 5, scale = 2)
    private Double completenessScore;

    @Column(name = "format_compliance_score", precision = 5, scale = 2)
    private Double formatComplianceScore;

    /**
     * 校验信息
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "format_validation", length = 20)
    private ExamNumberFormatValidation formatValidation;

    @Enumerated(EnumType.STRING)
    @Column(name = "student_match_status", length = 20)
    private ExamNumberStudentMatchStatus studentMatchStatus;

    @Column(name = "matched_student_id")
    private Long matchedStudentId;

    @Column(name = "validation_details", columnDefinition = "JSON")
    private String validationDetails;

    /**
     * 异常处理
     */
    @Column(name = "has_recognition_issues")
    private Boolean hasRecognitionIssues;

    @Column(name = "issue_details", columnDefinition = "JSON")
    private String issueDetails;

    @Column(name = "requires_manual_verification")
    private Boolean requiresManualVerification;

    /**
     * 时间信息
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    @Column(name = "last_verified_time")
    private LocalDateTime lastVerifiedTime;

    /**
     * 验证信息
     */
    @Column(name = "manual_verification_required")
    private Boolean manualVerificationRequired;

    @Column(name = "manual_verified_by")
    private Long manualVerifiedBy;

    @Enumerated(EnumType.STRING)
    @Column(name = "manual_verification_result", length = 20)
    private ExamNumberManualVerificationResult manualVerificationResult;

    @Column(name = "manual_corrected_number", length = 100)
    private String manualCorrectedNumber;

    @Column(name = "verification_notes", columnDefinition = "TEXT")
    private String verificationNotes;

    // 业务方法

    /**
     * 获取识别方法描述
     */
    public String getRecognitionMethodDescription() {
        return recognitionMethod != null ? recognitionMethod.getDescription() : null;
    }

    /**
     * 获取格式验证结果描述
     */
    public String getFormatValidationDescription() {
        return formatValidation != null ? formatValidation.getDescription() : null;
    }

    /**
     * 获取学生匹配状态描述
     */
    public String getStudentMatchStatusDescription() {
        return studentMatchStatus != null ? studentMatchStatus.getDescription() : null;
    }

    /**
     * 获取人工验证结果描述
     */
    public String getManualVerificationResultDescription() {
        return manualVerificationResult != null ? manualVerificationResult.getDescription() : null;
    }

    /**
     * 检查是否为手写识别方法
     */
}
